package no.kolstad.movies.controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import no.kolstad.movies.dao.MovieDAO;
import no.kolstad.movies.dao.UserDAO;
import no.kolstad.movies.model.Movie;
import no.kolstad.movies.model.User;
import no.kolstad.movies.service.MovieService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

/**
* Copyright 2008 Christopher Kolstad.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.

/***
 * @author chriswk - Christopher Kolstad
 */
 
public class EditMovieController extends SimpleFormController {
    private MovieService movieService;
    private MovieDAO movieDao;
    private UserDAO userDao;
    
    public void setMovieService(MovieService service) {
        this.movieService = service;
    }
    public void setMovieDAO(MovieDAO dao) {
        this.movieDao = dao;
    }
    public void setUserDAO(UserDAO dao) {
      userDao = dao;
    }
    protected Log log = LogFactory.getLog(this.getClass());
    
    protected Object formBackingObject(HttpServletRequest request)
      throws Exception {

    if (log.isInfoEnabled()) {
      log.info("EditMovieeController formBackingObject");
    }

    if (request.getParameter("movie_id") != null
        && !"".equals(request.getParameter("movie_id"))) {
      Long id = Long.parseLong(request.getParameter("movie_id"));
      Movie existingMovie = movieDao.get(id);
      return existingMovie;
    }

    return new Movie();
  }
    
    public ModelAndView onSubmit(HttpServletRequest request,
      HttpServletResponse response, Object command, BindException errors)
      throws ServletException {
    //Move command object into a Movie object for further manipulation
    Movie curMovie = (Movie) command;
    if(request.getParameter("user_id") != null && !"".equals(request.getParameter("user_id"))) {
      //Save the movie
      Long user_id = Long.parseLong(request.getParameter("user_id"));
      User currentUser = userDao.get(user_id);
      movieService.addMovie(currentUser, curMovie);
      return new ModelAndView(new RedirectView(getSuccessView()));
    }
    else {
      return new ModelAndView(new RedirectView("index.html")).addObject("error", "No user id was present in form for movie");
    }
    }
}
